package cuit.cx.groupcomm.util;

import java.util.Map;
import java.util.Set;

import net.sf.json.JSONObject;
import net.sf.json.JsonConfig;

public class JsonHelper
{
	
	
	
	
	
	public static JSONObject o2j(Object o, String[] ignoreFields)
	{
		JsonConfig config = new JsonConfig(); 
		if (ignoreFields != null)
		{
			config.setJsonPropertyFilter(new IgnoreFieldProcessorImpl(false, ignoreFields));
		}		
		return JSONObject.fromObject(o, config);
	}
	
	public static JSONObject o2j(Object o, String[] ignoreFields, boolean isIgnoreNull)
	{
		IgnoreFieldProcessorImpl ignore = new IgnoreFieldProcessorImpl(true, ignoreFields);
		ignore.setIgnoreNull(isIgnoreNull);
		JsonConfig config = new JsonConfig(); 	
		config.setJsonPropertyFilter(ignore);
		return JSONObject.fromObject(o, config);
	}
	
	
	
	public static String getResult(int result, Map<String,Object> map, Map<String, String[]> ignoreMap)
	{
		JSONObject resultObject = new JSONObject();
		resultObject.put("result", result);
		if (map != null)
		{
			Set<String> keys = map.keySet();
			for (String key : keys)
			{
				String[] ignore = null;
				if (ignoreMap != null)
				{
					ignore = ignoreMap.get(key);
				}
				Object o = map.get(key);
				resultObject.put(key, o2j(o, ignore));
			}
		}
		return resultObject.toString();		
	}
	
	public static String getResult(int result,String key, Object o, String[] ignore)
	{
		JSONObject resultObject = new JSONObject();
		resultObject.put("result", result);
		if (o != null)
		{
			JSONObject jsonObject = o2j(o, ignore);
			resultObject.put(key, jsonObject);
		}		
		return resultObject.toString();
	}
	
	
/*	public static String getUserinfoResult(int result, Userinfo userinfo)
	{
		return getResult(result, "userInfo", userinfo, new String[]{"password"});
	}
	
	public static String getRegisterUserinfoResult(Userinfo userinfo)
	{
		int result = Constant.ACK_FAIL;
		if (userinfo != null)
		{
			result = Constant.ACK_OK;
		}
		return getUserinfoResult(result, userinfo);
	}
	
	
	public static String getGroupJson(GroupDto dto)
	{
		JSONObject resultObject = new JSONObject();
	
		if (dto == null)
		{
			resultObject.put("result", Constant.ACK_FAIL);
		}
		else
		{
			resultObject.put("result", Constant.ACK_OK);
			
			
		}
		return resultObject.toString();
	}
	
	public static String getGroupListJson(List<GroupDto> list)
	{
		String[] ignoreFields = new String[]{"businissId","shopName","introdution","prompt","startTime","endTime","shopIntro","groupType"};
		JSONObject resultObject = new JSONObject();
		if (list == null)
		{
			resultObject.put("result", Constant.ACK_FAIL);
		}
		else
		{
			resultObject.put("result", Constant.ACK_OK);
			
			JSONArray jsonArray = new JSONArray();	
			for (GroupDto groupDto : list)
			{
				JSONObject object = o2j(groupDto, ignoreFields);
				jsonArray.add(object);
			}
			resultObject.put("groupons", jsonArray);		
		}
		return resultObject.toString();
	}
	
	
	public static String getGroupDetailJson(GroupDetail detail)
	{
		String[] orderIgnoreFields = new String[]{"groupon","userinfo","orderTime"};
		String[] picIgnoreFields = new String[]{"groupon"};
		JSONObject resultObject = new JSONObject();
		int result = Constant.ACK_FAIL;
		if (detail != null)
		{
			result = detail.getResult();
			
		}
		resultObject.put("result", detail.getResult());
		
		if (result == Constant.ACK_OK)
		{
			Groupon groupon = detail.getGroupon();
			List<Picture> pictures = detail.getPictures();
			List<Orders> orderinfos = detail.getOrderInfos();
			
			JSONObject grouponObject = o2j(groupon, null);
			resultObject.put("groupon", grouponObject);
			
			JSONArray picArray = new JSONArray();	
			for (Picture picture : pictures)
			{
				System.err.println(picture.getName());
				JSONObject object = o2j(picture, picIgnoreFields);
				picArray.add(object);
			}
			resultObject.put("pictures", picArray);	
			
			JSONArray orderArray = new JSONArray();	
			for (Orders orders : orderinfos)
			{
				JSONObject object = o2j(orders, orderIgnoreFields);
				orderArray.add(object);
			}
			resultObject.put("orderinfos", orderArray);	
			
		}
		
		return resultObject.toString();
		
	}
	
	
	public static String getQuserUserOrderJson(QueryUserOrder queryUserOrder)
	{
		IgnoreFieldProcessorImpl ignore = new IgnoreFieldProcessorImpl(false, null);
		ignore.setIgnoreNull(true);
		JsonConfig config = new JsonConfig(); 	
		config.setJsonPropertyFilter(ignore);
		return JSONObject.fromObject(queryUserOrder, config).toString();
		String[] serialIgnoreFields = new String[]{"orders","comment"};
		String[] orderIgnoreFields = new String[]{"serials"};
		JSONObject resultObject = new JSONObject();
		int result = Constant.ACK_FAIL;
		if (queryUserOrder != null)
		{
			result = queryUserOrder.getResult();
			
		}
		resultObject.put("result", queryUserOrder.getResult());
		
		if (result == Constant.ACK_OK)
		{
			List<OrderInfo> infos = queryUserOrder.getOrderInfos();
			
			JSONArray orderAarry = new JSONArray();
			for (OrderInfo orderInfo : infos)
			{
				List<Serial> serials = orderInfo.getSerials();
				JSONArray serialAarry = new JSONArray();
				for (Serial serial : serials)
				{				
					JSONObject object = o2j(serial, null, true);
					serialAarry.add(object);
				}
				
				JSONObject object = o2j(orderInfo, orderIgnoreFields);
				object.put("serials", serialAarry);
				orderAarry.add(object);
			}
			resultObject.put("orderInfos", infos);
		}
		
		return resultObject.toString();
	}*/
	
	
	
/*	public static Object j2o(String json)
	{
		
		JSONObject jsonObject = JSONObject.fromObject(json);
		Test test = (Test) JSONObject.toBean(jsonObject,Test.class);
		System.out.println(test.getUserInfo().getName());
		return null;
	}
	
	public static void main(String[] args)
	{
		String json = "{\"result\":1,\"userInfo\":{\"id\":21,\"createTime\":1333518482522,\"phoneNumber\":\"13980698040\",\"email\":\"jifver@qq.com\",\"name\":\"xsc\",\"power\":\"user\"}}";
	
		j2o(json);
	}*/

}
